package com.pkk.penguin.frame.know.shiro.shirodemo.springversion.controller;

import com.pkk.penguin.frame.know.shiro.shirodemo.springversion.entity.TbUserEntity;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import lombok.Data;
import org.springframework.web.bind.annotation.ModelAttribute;

/**
 * @description:
 * @author: peikunkun
 * @create: 2018-08-23 10:47
 **/
@Data
public class BaseController {

  /**
   * user-session的key
   */
  public static final String USER_SESSION = "USER_SESSION";

  protected HttpServletRequest request;
  protected HttpServletResponse response;
  protected HttpSession session;

  /**
   * @Description: 设置属性-在每次controll请求时先执行与方法
   * @Param: [request, response]
   * @return: void
   * @Author: peikunkun
   * @Date: 2018/8/23 0023 上午 10:52
   */
  @ModelAttribute
  public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
    this.request = request;
    this.response = response;
    this.session = request.getSession();
  }


  /**
   * 获得session用户对象
   *
   * @return
   */
  protected TbUserEntity getUser() {
    Object userObj = session.getAttribute(USER_SESSION);
    if (userObj == null) {
      return null;
    }
    return (TbUserEntity) userObj;
  }
}
